package com.fruits.login.service.impl;

import com.fruits.login.controller.HallController;
import com.fruits.login.dao.HallUrlDAO;
import com.fruits.login.dao.PlayerInfoDAO;
import com.fruits.login.model.*;
import com.fruits.login.model.properties.LoginProperties;
import com.fruits.login.service.CodeService;
import com.fruits.login.service.HallService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class HallServiceImpl implements HallService {
    private static final Logger logger = LoggerFactory.getLogger(HallController.class);

    @Autowired
    private CodeService codeService;

    @Autowired
    private PlayerInfoDAO playerInfoDAO;

    @Autowired
    private HallUrlDAO hallUrlDAO;

    private SmsCodeSingle smsCodeSingle = new SmsCodeSingle();
    @Override
    public Result<String> checkCode(PlayerLoginInfo hall) {
        logger.info(hall.getPhone() + "绑定手机号");
        ReturnMsg msg = smsCodeSingle.checkSmsCode(new SmsCode(hall.getCode(),hall.getPhone()));
        // 验证成功
        if (msg.getState() == LoginProperties.RETURN_MSG_SUCCESS){
            try{
                hall.setType(0);
                playerInfoDAO.updatePhone(hall);
                logger.info("大厅绑定手机号成功");
                return Result.SUCCESS();
            }catch (Exception e){
                logger.info("大厅绑定手机号失败");
                e.printStackTrace();
                return Result.valueOf(-1,"该手机号已绑定,请清除缓存使用手机号登录");
            }
        } else {
            logger.info("校验失败");
            return Result.valueOf(-1, msg.getInfo());
        }
    }

    @Override
    public boolean ipWhiteList(String ip) {
        if (IPWhiteList.getIps().contains(ip)){
            return true;
        }
        return false;
    }

    @Override
    public List<HallUrl> listOpenHall() {
        return hallUrlDAO.findAllHallHttpUrl();
    }
}
